import requests

host = 'localhost:8443'
# 登录接口
def login(username, password):
    url = "http://"+host+"/sso/app/login"
    data = {
        "username": username,
        "password": password
    }
    response = requests.post(url, data=data)
    if response.status_code == 200:
        result = response.json()
        # print(result)
        # {'code': 200, 'msg': None, 'data': '300_d97d0b6ec69b4dd0a56537ec7aa9ac1d'}
        if result.get('code') == 200:  # 假设登录成功的标志
            print("登录成功")
            return result.get('data')  # 返回登录成功的ticket
        else:
            print("登录失败:", result.get('msg'))
    else:
        print("请求失败, 状态码:", response.status_code)
    return None


# 检查接口
def check(ticket):
    url = "http://"+host+"/sso/app/check"
    params = {
        "ticket": ticket
    }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        result = response.json()
        # print(result)
        # {'code': 200, 'msg': None, 'data': {'username': '17745021111', 'plugininfo': None, 'id': '300', 'version': 'f6b59b2f74044a469cd54b5de3ca5c9a', 'expireMinute': 480, 'expireFreshTime': 1734406815989}}
        if result.get('code') == 200:  #
            print("检查成功，用户有效:"+str(result.get('data').get('username')))
        else:
            print("检查失败:", result.get('msg'))
    else:
        print("请求失败, 状态码:", response.status_code)

# 登出接口
def logout(ticket):
    url = "http://"+host+"/sso/app/logout"
    data = {
        "ticket": ticket
    }
    response = requests.post(url, data=data)
    if response.status_code == 200:
        result = response.json()
        print(result)
        if result.get('code') == 200:
            print("登出成功")
        else:
            print("登出失败:", result.get('msg'))
    else:
        print("请求失败, 状态码:", response.status_code)


if __name__ == "__main__":
    # 替换为你的实际用户名和密码
    username = "17745021111"
    password = "123456"

    # 登录
    ticket = login(username, password)
    if ticket:
        # 检查
        check(ticket)
        # 登出
        logout(ticket)
